package com.library_manager_system.utils;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import com.library_manager_system.Book;

public class Table {

	public static JTable createTable(ArrayList<Book> list){
		
		Object[][] cellData = new Object[list.size()][10];
		for(int x = 0;x<list.size();x++){
			for(int y = 0;y<10;y++){
				if(y==0){
					cellData[x][y] = list.get(x).getBook_name();
				}else if(y==1){
					cellData[x][y] = list.get(x).getPress();
				}else if(y==2){
					cellData[x][y] = list.get(x).getAuthor();
				}else if(y==3){
					cellData[x][y] = list.get(x).getPress_year();
				}else if(y==4){
					cellData[x][y] = list.get(x).getPress_time();
				}else if(y==5){
					cellData[x][y] = list.get(x).getISBN();
				}else if(y==6){
					cellData[x][y] = list.get(x).getBook_price();
				}else if(y==7){
					cellData[x][y] = list.get(x).getBook_number();
				}else if(y==8){
					cellData[x][y] = list.get(x).getBorrow_book_number();
				}else if(y==9){
					cellData[x][y] = list.get(x).getThe_rest_number();
				}
			}
			
		}

		String[] columnNames = {"书名", "出版社","主编","出版年份",
				"版次","ISBN","定价","图书馆总数","借出本数","现余本数"};

		JTable jtable = new JTable(cellData,columnNames);
		return jtable;
		
	}
	
	
	public static JTable makeFace(JTable jtable) {
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table,
						Object value, boolean isSelected, boolean hasFocus,
						int row, int column) {
					if (row % 2 == 0)
						setBackground(Color.white); // 设置奇数行底色
					else if (row % 2 == 1)
						setBackground(new Color(206, 231, 255)); // 设置偶数行底色
					return super.getTableCellRendererComponent(table, value,
							isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < jtable.getColumnCount(); i++) {
				jtable.getColumn(jtable.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return jtable;
	}
	
}
